package com.zbb.user.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zbb.base.model.Result;
import com.zbb.base.util.RedisUtil;
import com.zbb.user.mapper.SchoolMapper;
import com.zbb.user.model.po.School;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@Api(value = "查询学校校区",tags = "查询学校校区")
@RestController
public class SchoolController {
    public static final String SCHOOLS = "zbb:schools";
    @Autowired
    SchoolMapper schoolMapper;

    @Autowired
    RedisUtil redisUtil;

    @ApiOperation("学校列表查询")
    @GetMapping("/schools")
    public Result selectSchoolList(){
        List<Object> schools = redisUtil.lGet(SCHOOLS,0,-1);
        if (schools.isEmpty()) {
            QueryWrapper<School> schoolQueryWrapper = new QueryWrapper<>();
            schoolQueryWrapper.eq("type", 0);
            List<School> school1 = schoolMapper.selectList(schoolQueryWrapper);
            redisUtil.lSet(SCHOOLS, school1);
            return Result.ok(school1);
        }
        return Result.ok(schools.get(0));
    }

    @ApiOperation("校区列表查询")
    @GetMapping("/schoolInfo/{schoolId}")
    public String selectSchoolnfo(@PathVariable("schoolId") String schoolId){
       School school = schoolMapper.selectById(schoolId);
        return school.getSchoolName();
    }

    @ApiOperation("校区列表查询")
    @GetMapping("/campus/{schoolId}")
    public Result selectCampusList(@PathVariable("schoolId") Integer schoolId){
        QueryWrapper<School> schoolQueryWrapper = new QueryWrapper<>();
        schoolQueryWrapper.eq("type",1);
        schoolQueryWrapper.eq("school_id",schoolId);
        List<School> campus = schoolMapper.selectList(schoolQueryWrapper);
        return Result.ok(campus);
    }
}
